package java8.lambda.demo04lambda;

import java8.lambda.demo01.AbstractAppleBeforeTest;
import java8.lambda.demo01.Apple;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.function.*;

/**
 * @author ChangLiang
 * @date 2020/4/22
 */
public class LambdaExpressionTest extends AbstractAppleBeforeTest {

    @Test
    @DisplayName("previous handle method")
    public void test() {
        Comparator<Apple> byColor = new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getColor().compareTo(o2.getColor());
            }
        };

        list.sort(byColor);
        System.out.println(list);
    }


    @Test
    @DisplayName("test valid lambda")
    public void test3() {
        Function<String, Integer> function = (String s) -> s.length();

        Function<String, Integer> function2 = s -> s.length();

        Predicate<Apple> function1 = apple -> apple.getColor().equals("green");

        IntSupplier supplier = () -> 42;

        ObjIntConsumer handler = (x, y) -> {
            System.out.println(x);
            System.out.println(y);
        };
        handler.accept(1, 2);

        Consumer consumer = x -> System.out.println(x);
        consumer.accept(3);


        Runnable runnable = () -> {
        };

        Supplier<String> str = () -> "Hello";
        System.out.println(str.get());

        Function<Integer, String> str2 = i -> "Alex" + i;
        System.out.println(str2.apply(1));

        IntFunction<Double> f = i->i*100.0d;
        System.out.println(f.apply(10));

        Supplier<String> s = String::new;
        // out: empty string
        System.out.println(s.get());
    }
}
